package com.demo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.demo.core.AnnotationApplicationContext;
import com.demo.core.ApplicationContext;
import com.demo.entity.Echo;
import com.demo.entity.EchoImpl;
import com.demo.project.dao.AccountDataServer;
import com.demo.project.dao.PersistenceUtil;
import com.demo.project.dao.PersistentConfig;
import com.demo.project.dao.TeamDataServer;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class test {
    @Test
    public void test1(){
        ApplicationContext context = new AnnotationApplicationContext("com.demo");
        PersistenceUtil persistenceUtil = (PersistenceUtil) context.getBean("persistenceUtil");
        persistenceUtil.readDataFromFiles();
        System.out.println("teamDataServer = ");
        TeamDataServer teamDataServer = (TeamDataServer) context.getBean("teamDataServer");
        System.out.println("teamDataServer.getTeamMap() = " + teamDataServer.getTeamMap());
        System.out.println("teamDataServer.getTeamInfoMap() = " + teamDataServer.getTeamInfoMap());
        System.out.println("---------------");

        AccountDataServer accountDataServer = (AccountDataServer) context.getBean("accountDataServer");
        System.out.println("accountDataServer.getAccountIds() = " + accountDataServer.getAccountIds());
        System.out.println("accountDataServer.getAccountMap() = " + accountDataServer.getAccountMap());
        System.out.println("accountDataServer.getAccountWithTeam() = " + accountDataServer.getAccountWithTeam());


    }

    @Test
    public void test2(){
        AccountDataServer accountDataServer = new AccountDataServer();
        System.out.println("accountDataServer.getAccountMap().getClass().getTypeName() = " + accountDataServer.getAccountMap().getClass().getTypeName());
    }
    @Test
    public void test3(){
        Map<String,String> accountIds = new HashMap<>();
        accountIds.put("1","jack");
        accountIds.put("2","lucy");
        System.out.println(JSON.toJSONString(accountIds));

    }
    @Test
    public void test4(){
        Map<String,Student> studentMap = new HashMap<>();
        studentMap.put("1",new Student("jack",22));
        studentMap.put("2",new Student("lucy",18));
        studentMap.put("3",new Student("tomas",20));
        String s = JSON.toJSONString(studentMap);
        System.out.println("s = " + s);


        JSONObject jsonObject = JSON.parseObject(s);

        try {
        } catch (Exception e) {
            e.printStackTrace();
        }
        Iterator<Map.Entry<String, Object>> iterator = jsonObject.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String, Object> next = iterator.next();
            System.out.println("next.getKey() = " + next.getKey());
            System.out.println("next.getClass().getTypeName() = " + next.getClass().getTypeName());
            Student student = JSON.parseObject(next.getValue().toString(), Student.class);
            System.out.println("student = " + student);
        }
    }

    @Test
    public void test5(){

    }
}
